//
// Created by 文道江 on 15/12/21.
// Copyright (c) 2015 文道江. All rights reserved.
//

#import "HybridVideoCaptureAction.h"
#import "VideoPlayerViewController.h"


@implementation HybridVideoCaptureAction {

}

- (void)record:(id)params{

    id minTime = params[@"minTime"];
    id maxTime = params[@"maxTime"];

    CaptureViewController *captureViewController = [[CaptureViewController alloc] initWithNibName:@"CaptureViewController" bundle:nil];
    captureViewController.recorderDelegate = self;

    if (minTime){
        captureViewController.min_video_dur = [minTime floatValue];
    }
    if (maxTime){
        captureViewController.max_video_dur = [maxTime floatValue];
    }

    if (self.controller.navigationController){
        [self.controller.navigationController pushViewController:captureViewController animated:YES];
    }else{
        [self.controller presentViewController:captureViewController animated:YES completion:nil];
    }
}

- (void)play:(id)params{
    NSString *url = params[@"url"];
    if (!url){
        [self callback:NO data:@"URL参数错误."];
        return;
    }
    NSURL *videoURL = [NSURL URLWithString:url];
    //PlayViewController *playCon = [[PlayViewController alloc] initWithNibName:@"PlayViewController" bundle:nil withVideoFileURL:videoURL];
    VideoPlayerViewController *playCon = [VideoPlayerViewController controllerWithUrl:videoURL];
    playCon.autoPlay = YES;

    //playCon.payDelegate = self;
    if (self.controller.navigationController){
        [self.controller.navigationController pushViewController:playCon animated:YES];
    } else if (self.controller.presentedViewController){
        [self.controller presentViewController:playCon animated:YES completion:nil];
    }
}

#pragma mark VideoRecordDelegate

//完成
- (void)didFinish:(NSURL *)outputFileURL {
    [self callback:YES data:@{
            @"file": outputFileURL.relativePath,
            @"url": outputFileURL.absoluteString
    }];
    /*UploadManager *uploadManager = [UploadManager defaultManager];
    //[uploadManager uploadFile:outputFileURL];
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.controller.view animated:YES];
    hud.mode = MBProgressHUDModeDeterminateHorizontalBar;
    hud.labelText = @"上传中...";
    [uploadManager uploadFile:outputFileURL parameters:@{} completionHandler:^(NSURLResponse *response, id responseObject, NSError *error) {
        NSString *string = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
        [hud hide:YES];
    } sendBodyDataBlock:^(NSURLSession *session, NSURLSessionTask *task, int64_t bytesSent, int64_t totalBytesSent, int64_t totalBytesExpectedToSend) {
        hud.progress = totalBytesSent / totalBytesExpectedToSend * 100;
    }];*/
}

//取消
- (void)didCancel {
    [self callback:NO data:@"取消录像"];
}


@end